//
//  KLManageWithoutpwdController.m
//  KISSLOCK
//
//  Created by 华杨科技 on 2017/4/28.
//  Copyright © 2017年 陈宾鹏. All rights reserved.
//
//  免密管理

#import "KLManageWithoutpwdController.h"
#import "HemaConst.h"
#import "KLHelp.h"
#import "KLMianMiCell.h"
// 点击cell block回调
typedef void(^ClickCellBlock)(NSString *content,NSMutableArray *array);

@interface KLManageWithoutpwdController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *selectedArray;

@property (nonatomic, strong) NSMutableDictionary *dic;

@property (nonatomic, copy)ClickCellBlock click;

@end

@implementation KLManageWithoutpwdController
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableArray *)selectedArray {
    if (!_selectedArray) {
        _selectedArray = [NSMutableArray array];
    }
    return _selectedArray;
}
- (NSMutableDictionary *)dic {
    if (!_dic) {
        _dic = [NSMutableDictionary dictionary];
    }
    return _dic;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self setupSubviews];
    
    self.click = ^(NSString *content, NSMutableArray *array) {
    };
}

- (void)setupSubviews {
    
    [KLHelp navSetMianMi:self doBackHandler:^(UIButton *backBtn) {
        [backBtn addTarget:self action:@selector(doBack:) forControlEvents:(UIControlEventTouchUpInside)];
    } saveHandler:^(UIButton *saveBtn) {
        [saveBtn addTarget:self action:@selector(saveAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }];

    self.title = @"免密管理";
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    self.dataArray = @[@"放弃二级锁主权限",@"锁主校时",@"设置黑名单",@"取消黑名单",@"读取锁信息",@"分发钥匙",@"委托管理",@"支付密码管理",@"换锁芯",@"密码锁更改"].mutableCopy;
    
    
} - (void)doBack:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
} - (void)saveAction:(UIButton *)sender {
    self.click = ^(NSString *content, NSMutableArray *array) {
        NSLog(@"数据:%@ %@",content,array);
    };
}

#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    NSString *identifier = [NSString stringWithFormat:@"identifier%zd",indexPath.section];
    
    KLMianMiCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell) {
        cell = [[KLMianMiCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.titleLb.text = self.dataArray[indexPath.section];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    KLMianMiCell *cell = (KLMianMiCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    // 更新点击状态:取反
    [cell UpdateCellSelected:!cell.selected];
    
    if (cell.selected) {
        [self.selectedArray addObject:self.dic];
    }else {
        [self.selectedArray removeObject:cell.titleLb.text];
    }
    
    self.click(cell.titleLb.text, self.selectedArray);
}

@end
